<?php

class News extends CI_Controller {

    function __construct() {
        parent::__construct();
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
        } else {
            redirect('user_login/user', 'refresh');
        }
        $this->load->model('berita');
        $this->load->model('user');
        $this->load->model('user_role');
        $this->load->library('user_id');
        $this->load->helper('user_helper');
        if ($this->user_id->cek() == 0) {
            redirect('user_login/user', 'refresh');
        }
    }

    function index() {
        $data['id'] = $this->user_id->id();
        
        $data['i'] = 1;
        $config = array();
        $config["base_url"] = base_url() . "news/index";
        $config["total_rows"] = $this->berita->record_count();
        $config["per_page"] = 10;
        $config["uri_segment"] = 3;

        $this->pagination->initialize($config);

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data["rows"] = $this->berita->fetch_costumer($config["per_page"], $page);
        $data["links"] = $this->pagination->create_links();

        $data['content'] = 'news/index';
        $this->load->view('template/user', $data);
    }

    function show() {
        $data['id'] = $this->user_id->id();
        
        $id = $this->uri->segment(3);
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data['rows'] = $this->berita->show($id);
        $data['content'] = 'news/show';
        $this->load->view('template/user', $data);
    }

    function add() {
        $data['id'] = $this->user_id->id();
        
        #cek
        $access = $this->user_role->access($this->user_id->cek());
        $this->user_role->chek($access, 'news_add');
        
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data['content'] = 'news/new';
        $this->load->view('template/user', $data);
    }

    function edit() {
        $data['id'] = $this->user_id->id();
        
        $id = $this->uri->segment(3);
        #cek
        $access = $this->user_role->access($this->user_id->cek());
        $this->user_role->chek($access, 'news_edit');
        
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data['rows'] = $this->berita->edit($id);
        $data['content'] = 'news/edit';
        $this->load->view('template/user', $data);
    }

    function create() {
        $date = date('Y-m-d');
        $title = $this->input->post('title', TRUE);
        $body = $this->input->post('body', TRUE);
        $this->berita->create($title, $body, $date);
    }

    function update() {
        $id = $this->input->post('id', TRUE);
        $title = $this->input->post('title', TRUE);
        $body = $this->input->post('body', TRUE);
        $date = date('Y-m-d');
        $this->berita->update($id, $title, $body, $date);
    }

    function delete() {
//        $access = $this->user_role->access($this->user_id->cek());
//        $this->user_role->chek($access, 'delete');

        $id = $this->uri->segment(3);
        $this->berita->delete($id);
    }
}

?>
